%----------------------------------------------
% Formatting and file path
%----------------------------------------------

clear;
clc;
format bank; % Set formatting to display numbers without e format

%----------------------------------------------
% Read in data
%----------------------------------------------
Folder = cd; 
Folder = fullfile(Folder, '../../Data/structural/');
cd(Folder); 
load('output/Structural_Results_at_ss_assets_minus_land_of_rich.mat')

data = csvread('intermediate/w5_capital_of_stups.csv');
hhid=data(:,1);
k_w5=data(:,4);
l_w5=data(:,2);
h_w5=data(:,3);
hired_l_w5=data(:,5);
w_w5=data(:,6);
N=length(hhid);

%----------------------------------------------
% Simplify terms
%----------------------------------------------

fk_w5 = a.*k_w5.^2 + b.*k_w5;


%----------------------------------------------
% Estimate h*, l*, h' at wave 5 capital level
%----------------------------------------------

psi_l_sim = ratio_psi_l.*psi_l;
psi_h_sim = ratio_psi_l.*psi_h;

l_opt_w5 = zeros(N,1);
h_opt_w5 = zeros(N,1);
hiredin_opt_w5 = zeros(N,1);
profit_opt_w5 = zeros(N,1);

for i=1:N
    profit = @(x) -A(i)*fk_w5(i)*(x(1) + x(3))^beta - w_w5(i)*x(2) + phi*w_w5(i)*x(3) + 0.5*(sqrt(psi_l_sim(i))*x(1) + sqrt(psi_h_sim(i))*x(2))^2;
    [argmax, max] = fmincon(profit, [0,0,0], [1,1,0], R, [], [], [0,0,0], [R,H(i),Hire_in_max]);
    l_opt_w5(i) = argmax(1);
    h_opt_w5(i) = argmax(2);
    hiredin_opt_w5(i) = argmax(3);
    profit_opt_w5(i) = -max;
end

clear max argmax

% Check if it would be optimal to liquidate capital:
for i=1:N
    profitalt = @(x) -rho*k_w5(i) - w_w5(i)*x + 0.5*psi_h_sim(i)*x^2;
    [argmax, max] = fmincon(profitalt, 0, [], [], [], [], 0, H(i));
    if -max > profit_opt_w5(i)
        l_opt_w5(i) = 0;
        hiredin_opt_w5(i) = 0;
        h_opt_w5(i) = argmax;
        profit_opt_w5(i) = -max;
    end
end

clear max argmax

% Trim :
l_opt_w5(l_opt_w5 < 1) = 0;
h_opt_w5(h_opt_w5 < 1) = 0;
hiredin_opt_w5(hiredin_opt_w5 < 1) = 0;

% Optimal cases:
case_opt_w5 = zeros(N,1);

for i=1:N
    if l_opt_w5(i) > 0 && h_opt_w5(i) > 0 && hiredin_opt_w5(i) > 0
        case_opt_w5(i) = 1;
    elseif l_opt_w5(i) > 0 && h_opt_w5(i) > 0 && hiredin_opt_w5(i) == 0
        case_opt_w5(i) = 2;
    elseif l_opt_w5(i) > 0 && h_opt_w5(i) == 0 && hiredin_opt_w5(i) > 0
        case_opt_w5(i) = 3;
    elseif l_opt_w5(i) > 0 && h_opt_w5(i) == 0 && hiredin_opt_w5(i) == 0
        case_opt_w5(i) = 4;
    elseif l_opt_w5(i) == 0 && h_opt_w5(i) > 0 && hiredin_opt_w5(i) > 0
        case_opt_w5(i) = 5;
    elseif l_opt_w5(i) == 0 && h_opt_w5(i) > 0 && hiredin_opt_w5(i) == 0
        case_opt_w5(i) = 6;
    elseif l_opt_w5(i) == 0 && h_opt_w5(i) == 0
        case_opt_w5(i) = 7;
    end
end

%----------------------------------------------
% Determine actual case in wave 5
%----------------------------------------------

case_real_w5 = zeros(N,1);

for i=1:N
    if l_w5(i) > 0 && h_w5(i) > 0 && hired_l_w5(i) > 0
        case_real_w5(i) = 1;
    elseif l_w5(i) > 0 && h_w5(i) > 0 && hired_l_w5(i) == 0
        case_real_w5(i) = 2;
    elseif l_w5(i) > 0 && h_w5(i) == 0 && hired_l_w5(i) > 0
        case_real_w5(i) = 3;
    elseif l_w5(i) > 0 && h_w5(i) == 0 && hired_l_w5(i) == 0
        case_real_w5(i) = 4;
    elseif l_w5(i) == 0 && h_w5(i) > 0 && hired_l_w5(i) > 0
        case_real_w5(i) = 5;
    elseif l_w5(i) == 0 && h_w5(i) > 0 && hired_l_w5(i) == 0
        case_real_w5(i) = 6;
    elseif l_w5(i) == 0 && h_w5(i) == 0
        case_real_w5(i) = 7;
    end
end


%----------------------------------------------
% Estimate value of misallocation at wave 5
%----------------------------------------------

misallocation_w5 = zeros(N,1);

for i=1:N
    if case_opt(i) == 5 || case_opt(i) == 6   % Misallocation is zero if case 5 or 6 dominates at high SS
        misallocation_w5(i) = 0;
    else
        misallocation_w5(i) = profit_opt(i) - profit_opt_w5(i);
    end
end

misallocation_w5(misallocation_w5 < 0) = 0;

% Top-code top 5% outliers
misallocation_95_pcile_w5 = prctile(misallocation_w5,95);
misallocation_topcoded_w5 = misallocation_w5;
misallocation_topcoded_w5(misallocation_topcoded_w5>misallocation_95_pcile_w5) = misallocation_95_pcile_w5;

total_misallocation_w5=sum(misallocation_w5);
total_misallocation_topcoded_w5=sum(misallocation_topcoded_w5);

% Calculate total number of individuals misallocated

num_misallocated_w5 = sum(misallocation_w5(:) ~= 0);


%----------------------------------------------
% Export results to Excel
%----------------------------------------------

T=table(hhid,case_opt_w5,case_real_w5,l_opt_w5,l_w5,h_opt_w5,h_w5,hiredin_opt_w5,hired_l_w5,k_w5);
filename='output/w5_test_of_model.xlsx';
writetable(T,filename);

save('output/w5_test_results')
